package LoginUtil;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import com.db4o.*;
//import com.db4o.cs.Db4oClientServer;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {

	

public void doGet(HttpServletRequest request, HttpServletResponse response) 
			           throws ServletException, java.io.IOException {
	try
	{	 
		UserBean usr = UserDAO.login(request.getParameter("usernamebox"), request.getParameter("passwordbox"));
		
		if(usr.isValid()) {
			HttpSession session = request.getSession(true);	    
	        session.setAttribute("currentSessionUser",usr); 
	        
	        
	        if(usr.getTypeForDB() == 2)
	        {
	      	  response.sendRedirect("doctorMain.jsp"); //logged-in page  for doctors
	        }
	        else if(usr.getTypeForDB() == 3)
	        {
	      	  response.sendRedirect("nurseMain.jsp"); //logged-in page for nurses
	        }
	        else if(usr.getTypeForDB() == 4)
	        {
	      	  response.sendRedirect("patientMain.jsp"); //logged-in page for nurses
	        }
	        else if(usr.getTypeForDB() == 1)
	        {
	      	  response.sendRedirect("adminMain.jsp"); //logged-in page for nurses
	        }
	        else
	        {
	      	  response.sendRedirect("userLogged.jsp"); //logged-in page 
	        }
	   }
		        
	   else 
	        response.sendRedirect("invalidLogin.jsp"); //error page 
			
		
		/*
	     UserBean user = new UserBean();
	     user.setUserName(request.getParameter("usernamebox"));
	     user.setPassword(request.getParameter("passwordbox"));
	
	     user = UserDAO.login(user);
		   		    
	     if (user.isValid())
	     {
		        
	          HttpSession session = request.getSession(true);	    
	          session.setAttribute("currentSessionUser",user); 
	          //set user type here
	          user.setType("doctor");
	          
	          if(user.getType().compareToIgnoreCase("doctor") == 0)
	          {
	        	  response.sendRedirect("doctorMain.jsp"); //logged-in page  for doctors
	          }
	          else if(user.getType().compareToIgnoreCase("nurse") == 0)
	          {
	        	  response.sendRedirect("nurseMain.jsp"); //logged-in page for nurses
	          }
	          else if(user.getType().compareToIgnoreCase("patient") == 0)
	          {
	        	  response.sendRedirect("patientMain.jsp"); //logged-in page for nurses
	          }
	          else if(user.getType().compareToIgnoreCase("ADMIN") == 0)
	          {
	        	  response.sendRedirect("adminMain.jsp"); //logged-in page for nurses
	          }
	          else
	          {
	        	  response.sendRedirect("userLogged.jsp"); //logged-in page 
	          }
	     }
		        
	     else 
	          response.sendRedirect("invalidLogin.jsp"); //error page 
	} 
	*/
	} catch (Throwable theException) {
	     System.out.println(theException); 
	}
  }
}
